<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			*{margin:0;padding:0;box-sizing: border-box;}
			.wrap{
				width:600px;
				height:600px;
				margin: auto;
				position:absolute;
				top:0;bottom:0;
				right:0;left:0;
			}
			span{position:relative;font-weight: bold;font-size:32px;display:inline-block;text-align: center;width:60px;height:60px; background:black;color:white;line-height:60px;}
			input{position:absolute;font-size:32px;color:black;font-weight: bold;height:100%;width:100%;top:0;left:0;line-height:60px;text-align: center;}
			.red{color:red}
		</style>
		<script src="js/elf-min-2-0.js"> </script>
	</head>
	<body>
		<script>
			Len = 9;Q = 0;nandu = 8;
			function createNineTable(){
				 function createNumber(){
			 		return Math.floor(Math.random()*Len+1);
		 		 };
			 	 function getRow(r){
				 	var arr  = [];
				 	Number.forEach(function(v){
				 		if(v[r]){
				 			arr.push(v[r]);
				 		};
				 	});
				 	return arr;
				 };
				  function isLive(arr,arr1,arr2){
				 	var v = createNumber();
				 	var val = 0;	
				 		arr2.forEach(function(q){
				 			if(q==v){
								val++;			 				
				 			};
				 		});
				 		if(arr.indexOf(v)==-1&&arr1.indexOf(v)==-1&&val==1){
				 			return v;
				 		};
						return isLive(arr,arr1);		 			
				 };
				 function getNineArr(c,r){
				 	if(c/3<1){
				 		if(r/3<1){
				 			
				 		}else if(r/3<2){
				 			
				 		}else if(r/3<3){
				 			
				 			
				 		};
				 	}else if(c/3<2){
				 		if(r/3<1){
				 			
				 		}else if(r/3<2){
				 			
				 		}else if(r/3<3){
				 			
				 			
				 		};
				 	}else if(c/3<3){
				 		if(r/3<1){
				 			
				 			
				 		}else if(r/3<2){
				 			
				 		}else if(r/3<3){
				 			
				 			
				 		};
				 	};
				 };
				 function isSuccesss(n,arr,arr1){
				 		var num  = 0;
				 		arr.forEach(function(v,k){
				 			if(n==v){
				 				num++;
				 			};
				 		});
				 		if(num!=1){
				 			return false;
				 		};
				 		num = 0;
				 		arr1.forEach(function(v,k){
				 			if(n==v){
				 				num++;
				 			};
				 		});
				 		if(num!=1){
				 			return false;
				 		};
				 		return true;
				 };
				  function fillArr(arr,num){
				 	arr.push(num);
				 };
				
			
			try{
				 var Number = [];Q++;
				 for(var q = 0;q<Len;q++){
				 	Number.push([]);
				 };
				 var x = 0;
				 while(Number[Len-1].length!=Len){
				 	if(Number[x].length==Len){
				 		x++;
				 	};
				 	if(Number[0].length==0){
				 		Number[x].push(createNumber());
				 	};
				 	fillArr(Number[x],isLive(Number[x],getRow(Number[x].length)));
				 };
				 var wrap = Elf.controls.createElement("div",document.body,"wrap");
				 Number.forEach(function(v,k){
				 	var div = Elf.controls.createElement("div",wrap);
						v.forEach(function(v,kk){
						var span =	Elf.controls.createElement("span",{innerHTML:v},div);
							if(createNumber()>nandu){
								var inpur = Elf.controls.createElement("input",span,{type:"text"});
								inpur.r=kk;
								inpur.c=k;
								inpur.oninput=function(e){

									if(this.value!=this.parentElement.childNodes[0].nodeValue){
										this.className='red';
									}else{
										this.className="";
									};
									console.log(this.parentElement.childNodes[0].nodeValue);
									console.log(this.r,this.c);
								};
								inpur.onblur=function(){
									var inputs = document.getElementsByTagName("input");
									var isValue = true;
									Elf.utils.each(inputs,function(k,v){
										if(!v.value){
											isValue = false;
										}
									});
									if(isValue){
										var isSuccess = true;
										var isSuccessPd =  [];
										Elf.utils.each(inputs,function(k,v){
				 							console.log(getRow(v.r));
											isSuccessPd[isSuccessPd.length]=isSuccesss(v.value,Number[v.c],getRow(v.r));
										});
										if(isSuccessPd.every(function(v){
											return v==true;
										})){
											alert("成功!");
										}else{
										}
									};
								};
							};
						});
				 });
				 console.log(Q);
			}catch(e){
				//TODO handle the exception
				createNineTable();
			}
			};
			createNineTable()
		</script>		
	</body>
</html>
